$(function () {
    loading();
    ajaxfriendsManager(1);
    $(".liBox li a").click(function () {
        $("#info").empty();
        loading();
        $(this).each(function () {
            $(".liBox li a").prop("class", "");
            $(this).prop("class", "cur");
            var text = $(this).prop("text");
            if (text == 'All')
                ajaxfriendsManager(1);
            else
                findByFl(text);
        })
    });

});

/**
 * 加载好友列表
 * @param page 当前页码（为空时显示第一页）
 */
function ajaxfriendsManager(page) {
    var url = "/Lvscore_User_M/account/ajaxfriendsManager";
    $.post(
        url,
        {'page': page},
        function (data) {
            loadData(data);
            //分页
            if ($(".tcdPageCode").html() == "") {
                createPage(data.sumpage, data.nowpage);
            }
        },
        "json"
    );
}

function getImageUrl(url) {
    if (url == '' || url == null) {
        return "/Lvscore_User_M/userWeb/static/images/manageWrap_pic.png";
    } else {
        return url;
    }
}

function findByFl(f) {
    ajaxFriendsByFirstLetter(1, f);
}


/**
 * 按首字母A-Z查询我的朋友
 * @param page    当前页码（需要加载哪一页的数据）
 * @param first    首字母A-Z
 */
function ajaxFriendsByFirstLetter(page, first) {
    var url = "/Lvscore_User_M/account/ajaxFriendsByFirstLetter";
    $.post(
        url,
        {
            'page': page,
            'first': first
        },
        function (data) {
            loadData(data);
            //分页
            if (data.nowpage == 1) {
                $(".tcdPageCode").empty().unbind();
            }
            if ($(".tcdPageCode").html() == "") {
                createPageFind(data.sumpage, data.nowpage, first);
            }
        },
        "json"
    );
}

function loadData(data) {
    $("#info").empty();
    $("#info_head").empty();
    if (data.friendList.length == 0) {
        $("#info").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">未查询到任何数据</p>');
        $(".loadingBox").hide();
    }else{
        $("#info_head").append(
        '<tr class="tabelHeader" style="height:30px;background:#fff;font-size:18px;">'+
            '<th class="small" style="width:130px;">用户头像</th>'+
            '<th class="small">用户名称</th>'+
            '<th class="small">ID</th>'+
            '<th class="small">城市</th>'+
            '<th></th>'+
        '</tr>'
        );
    }
    $.each(data.friendList, function (i, e) {
        $("#info").append(
            '<tr style="height:100px;">' +
            '<td class="small" style="width:130px;"><img class="iconImg" src="' + getImageUrl(e.image_url) + '"></td>' +
            '<td class="small">' + e.name + '</td>' +
            '<td class="small">' + e.devId + '</td>' +
            '<td class="small">' + stringUtil(e.area) + '</td>' +
                //'<td class="big">' + stringUtil(e.sign) + '</td>' +
            '<td><a href="/Lvscore_User_M/userWeb/views/accounterCenter/friendDetail.html?id=' + e.devId + '" class="detail">好友详情</a></td>' +
            '</tr>'
        );

        $(".loadingBox").hide();
    });
}

function setCook() {
    var info = $("#name").val();
    if (info == null || info == 'null')  info = '';
    var json = JSON.stringify(info);
    var cookie = new sweetCookie();
    cookie.setCookie("searchF", json);
    return true;
}
function searchFriend() {
    if (isNotEmpty($("#name").val()) && setCook())
        window.location.href = '/Lvscore_User_M/userWeb/views/accounterCenter/addFriend.html';
}
function createPage(pages, pageNum) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxfriendsManager(p);
            }
        });
    }
}

function createPageFind(pages, pageNum, first) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxFriendsByFirstLetter(p, first);
            }
        });
    }
}

function searchEnter(e) {
    if (isNotEmpty($("#name").val())) {
        var key = window.event ? e.keyCode : e.which;
        if (key == 13) {
            searchFriend();
        }
    }
}